﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using FITClub.TextureMenu;
using Microsoft.Xna.Framework.Graphics;

#if WINDOWS

using System.Drawing.Design;
using System.ComponentModel;
using FITClub.Episode1.Levels;

#endif

namespace FITClub.Episode1.Configuration
{
    public class TextureItem : ILoadable
    {
#if WINDOWS

        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        [XmlAttribute]
        public string Path { get; set; }

        [XmlAttribute]
        public string MeshId { get; set; }

        [XmlIgnore]
#if WINDOWS
        [Browsable(false)]
#endif
        public Texture2D Texture { get; private set; }

        [XmlIgnore]
#if WINDOWS
        [Browsable(false)]
#endif
        public bool ContentLoaded { get; set; }

        public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (ContentLoaded == false)
            {
                try /// tring in order to not to include unused or invisible textures in content
                {
                    Texture = loader.Load<Texture2D>(this.Path);
                    ContentLoaded = true;
                }
                catch
                {
                }
            }
        }

        public void Dispose()
        {
            if (ContentLoaded == true)
            {
                Texture.Dispose();
                ContentLoaded = false;
            }
        }

        public override string ToString()
        {
            return string.Format("meshId={0}, path={1}", this.MeshId, this.Path);
        }
    }
}